﻿using DotNetCore.CAP;
using DotNetCore.CAP.Transport;
using SqlSugar;

namespace CapHttp.Core;

public class SqlSugarCapTransaction : CapTransactionBase
{
    public IAdo Ado { get; set; }
    
    public SqlSugarCapTransaction(IDispatcher dispatcher, IAdo ado) : base(dispatcher)
    {
        Ado = ado;
        DbTransaction = ado.Transaction;
    }

    public override void Commit()
    {
        Ado.CommitTran();
        Flush();
    }

    public override async Task CommitAsync(CancellationToken cancellationToken = new CancellationToken())
    {
        await Ado.CommitTranAsync();
        Flush();
    }

    public override void Rollback()
    {
        Ado.RollbackTran();
    }

    public override async Task RollbackAsync(CancellationToken cancellationToken = new CancellationToken())
    {
        await Ado.RollbackTranAsync();
    }

    public override void Dispose()
    {
        Ado.Dispose();
    }
}